﻿using System;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using CDB.LinqModel.Data;
using CDB.LinqData;
using CDB.LinqModel;
using System.Web.UI.WebControls;

public partial class back_ViewBuilders : System.Web.UI.Page
{
    #region Variable Declaration
    IDataRepository _dataObject = new DataRepository();
    Builder builder;
    List<Builder> builders = new List<Builder>();
    #endregion //Variable Declaration
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bindBuildersGrid();
        }
    }

    private void bindBuildersGrid()
    {
        builder = new Builder(_dataObject);
        builders = builder.GetAllBuilders();

        if (builders.Count > 0)
        {
            grdBuilders.Visible = true;
            grdBuilders.DataSource = builders;
            grdBuilders.DataBind();
        }
        else
        {
            grdBuilders.Visible = false;
            divRed.InnerHtml = "<strong>Oh snap!</strong>&nbsp;No Records Found!";
            divRed.Attributes.Add("class", "alert alert-error");
        }
    }
    protected void grdBuilders_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdBuilders.PageIndex = e.NewPageIndex;
        bindBuildersGrid();
    }
    protected void grdBuilders_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.ToString().Trim().ToLower() == "editbuilder")
        {
            System.Web.UI.WebControls.GridViewRow row = (System.Web.UI.WebControls.GridViewRow)(((System.Web.UI.WebControls.Button)e.CommandSource).NamingContainer);
            System.Web.UI.WebControls.Button btnEdit = (System.Web.UI.WebControls.Button)row.FindControl("btnEdit");
            Response.Redirect(btnEdit.PostBackUrl);
        }
        if (e.CommandName.ToString().ToLower().Trim().Equals("deletebuilder"))
        {
            System.Web.UI.WebControls.GridViewRow row = (System.Web.UI.WebControls.GridViewRow)(((System.Web.UI.WebControls.Button)e.CommandSource).NamingContainer);
            System.Web.UI.WebControls.Label hdnBuilderId = (System.Web.UI.WebControls.Label)row.FindControl("hdnBuilderId");
            builder = new Builder(_dataObject);
            builder.Id = Convert.ToInt32(hdnBuilderId.Text);
            builder.Delete();
            bindBuildersGrid();
            divRed.InnerHtml = "<strong>Deletion!</strong>&nbsp;Record Deleted Successfully!";
            divRed.Attributes.Add("class", "alert alert-error");
        }
    }
    protected void grdBuilders_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == System.Web.UI.WebControls.DataControlRowType.DataRow)
        {
            System.Web.UI.WebControls.Label hdnBuilderId = (System.Web.UI.WebControls.Label)e.Row.FindControl("hdnBuilderId");
            System.Web.UI.WebControls.Button btnEdit = (System.Web.UI.WebControls.Button)e.Row.FindControl("btnEdit");
            System.Web.UI.WebControls.Button btnBuilderWorks = (System.Web.UI.WebControls.Button)e.Row.FindControl("btnBuilderWorks");
            btnEdit.PostBackUrl = "AddBuilders.aspx?id=" + hdnBuilderId.Text;
            btnBuilderWorks.PostBackUrl = "builderWork.aspx?Id=" + hdnBuilderId.Text;
        }
    }
}